package com.suse.warmstill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.warmstill.common.enums.ResponseEnum;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.entity.TbResource;
import com.suse.warmstill.entity.TbRoleResource;
import com.suse.warmstill.mapper.TbResourceMapper;
import com.suse.warmstill.mapper.TbRoleResourceMapper;
import com.suse.warmstill.service.BaseService;
import com.suse.warmstill.service.ResourceService;
import com.suse.warmstill.vo.ConditionVO;
import com.suse.warmstill.vo.ResourceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 黄靖
 * @author 2022-10-21 14:09
 * description:
 **/
@Service
@Slf4j
public class ResourceServiceImpl implements ResourceService, BaseService<ResourceVo, TbResource> {

    @Autowired
    private TbRoleResourceMapper roleResourceMapper;

    @Autowired
    private TbResourceMapper resourceMapper;


    @Override
    public TbResource toEntity(ResourceVo vo) {

        final TbResource tbResource = new TbResource();
        if (ObjectUtils.isNotEmpty(vo.getId())) {
            tbResource.setId(vo.getId());
        }
        tbResource.setResourceName(vo.getResourceName());
        tbResource.setIsAnonymous(vo.getIsAnonymous());
        tbResource.setUrl(vo.getUrl());
        tbResource.setParentId(vo.getParentId());
        tbResource.setRequestMethod(vo.getRequestMethod());


        return tbResource;
    }

    @Override
    public ResourceVo toVo(TbResource entity) {
        final ResourceVo resourceVo = new ResourceVo();
        resourceVo.setResourceName(entity.getResourceName());
        resourceVo.setId(entity.getId());
        resourceVo.setIsAnonymous(entity.getIsAnonymous());
        resourceVo.setParentId(entity.getParentId());
        resourceVo.setRequestMethod(entity.getRequestMethod());
        resourceVo.setUrl(entity.getUrl());
        return resourceVo;
    }

    @Override
    public ListPageModel<ResourceVo> listsResource(ConditionVO conditionVO) {
        final Page<TbResource> page = new Page<TbResource>(conditionVO.getCurrent(), conditionVO.getSize());
        final LambdaQueryWrapper<TbResource> tbResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(conditionVO.getKeyword())) {
            tbResourceLambdaQueryWrapper.eq(TbResource::getResourceName, conditionVO.getKeyword());
        }
        final Page<TbResource> tbResourcePage = resourceMapper.selectPage(page, tbResourceLambdaQueryWrapper);
        final ArrayList<ResourceVo> resourceVos = new ArrayList<>();
        tbResourcePage.getRecords().stream().forEach((tbResource -> {
            ResourceVo resourceVo = toVo(tbResource);
            resourceVos.add(resourceVo);

        }));

        final ListPageModel<ResourceVo> resourceVoListPageModel = new ListPageModel<>();
        resourceVoListPageModel.setList(resourceVos);
        resourceVoListPageModel.setCurrentPage((int) tbResourcePage.getCurrent());
        resourceVoListPageModel.setPageCount((int) tbResourcePage.getPages());
        resourceVoListPageModel.setPageSize((int) tbResourcePage.getSize());
        resourceVoListPageModel.setDataSize((int) tbResourcePage.getTotal());


        return resourceVoListPageModel;
    }


    @Override
    public ResourceVo getOneById(Long resourceId) {
        final TbResource tbResource = resourceMapper.selectById(resourceId);
        if (ObjectUtils.isEmpty(tbResource)) {
            throw new BusinessException(ResponseEnum.NO_OBJECT.getCode(), ResponseEnum.NO_OBJECT.getMsg());
        }
        final ResourceVo resourceVo = toVo(tbResource);
        return resourceVo;
    }

    @Override
    public void insertOne(ResourceVo resourceVo) {
        final TbResource tbResource = toEntity(resourceVo);
        resourceMapper.insert(tbResource);
    }

    @Override
    public void deleteOneOrMany(List<Long> rouseIds) {

        for (Long id : rouseIds
        ) {
            resourceMapper.deleteById(id);
            final LambdaQueryWrapper<TbRoleResource> tbRoleResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tbRoleResourceLambdaQueryWrapper.eq(TbRoleResource::getResourceId, id);
            roleResourceMapper.delete(tbRoleResourceLambdaQueryWrapper);
        }
    }

    @Override
    public void updateOne(ResourceVo resourceVo) {
        final TbResource tbResource = toEntity(resourceVo);
        resourceMapper.updateById(tbResource);
    }
}
